//
//  HRMyPreferentViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-8-11.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRMyPreferentViewController.h"
#import "PPiFlatSegmentedControl.h"
#import "HRMyPreferentTableViewCell.h"
#import "HRCouponListModel.h"

@interface HRMyPreferentViewController (){
    NSMutableArray *arrayTable;
    NSMutableArray *arrayCanUse;      //可用优惠券
    NSMutableArray *arrayHistory;   //已过期
    
    BOOL canUse;      //是否可用，用来确定cell图片
    PPiFlatSegmentedControl *segment;
}

@end

@implementation HRMyPreferentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        arrayTable = [[NSMutableArray alloc] init];
        arrayCanUse = [[NSMutableArray alloc] init];
        arrayHistory = [[NSMutableArray alloc] init];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    self.navigationItem.title = @"优惠券";
    canUse = YES;
    
    [self addSegment];
    [self loadData];
    
}

//获取数据
-(void)loadData{
    
    //隐藏为空提示页面
    [self hideHud];
    
    //清空数据
    [arrayTable removeAllObjects];
    
    [self showCustomLoadingHud:kLoading inView:nil];
    [HRInterfaceManager getCouponList:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            HRCouponListModel *list = (HRCouponListModel*)data;
            NSArray *array = [NSArray arrayWithArray:list.list];
            for (HRCouponInfoModel *info in array) {
                if ([info.isUsed isEqualToString:@"0"] &&[info.isGive isEqualToString:@"1"] &&[info.isOverdueDate isEqualToString:@"0"]) {
                    [arrayCanUse addObject:info];
                }else{
                    [arrayHistory addObject:info];
                }
            }
            
            [arrayTable addObjectsFromArray:arrayCanUse];
            [_tableView reloadData];
            
        }else if([message isEqualToString:NO_WIFI_ERROR_CODE]){
            //显示没有联网提示
            [self showHudByType:HUDTYPE_NO_WIFI];
            return;
        }else{
            [self showTextHUD:message];
        }
        
        if ([arrayCanUse count]==0 && [arrayHistory count]==0) { //没有优惠券
            //显示数据为空提示
            [self showHudByType:HUDTYPE_COUPON];
        }else{
            if ([arrayCanUse count] ==0) { //有历史优惠券
                [segment setEnabled:YES forSegmentAtIndex:1];
                canUse = NO;
                [arrayTable addObjectsFromArray:arrayHistory];
                [_tableView reloadData];
            }
        }
        
        //页面加载耗时
        [self analysisEnd:nil];
        
    }];
    
}



//成长值/规则切换
-(void)addSegment{
    
    segment = [[PPiFlatSegmentedControl alloc]initWithFrame:_viewSegmentBg.bounds items:@[ @{@"text":@"当前"},@{@"text":@"历史"}] iconPosition:IconPositionRight andSelectionBlock:^(NSUInteger segmentIndex) {
        [arrayTable removeAllObjects];
        if (segmentIndex == 0) {
            canUse = YES;
            [arrayTable addObjectsFromArray:arrayCanUse];
            [_tableView reloadData];
        }else{
            canUse = NO;
            [arrayTable addObjectsFromArray:arrayHistory];
            [_tableView reloadData];
        }
    }];
   
    segment.color = [UIColor clearColor];
    segment.borderWidth=0.5;
    segment.borderColor= kAppActiveColor;
    segment.selectedColor= kAppActiveColor;
    segment.textAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:13],
    NSForegroundColorAttributeName:kAppActiveColor};
    segment.selectedTextAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:13],
                                     NSForegroundColorAttributeName:[UIColor whiteColor]};
    [_viewSegmentBg addSubview:segment];
}

#pragma mark - TableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrayTable.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HRMyPreferentTableViewCell";
    HRMyPreferentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HRMyPreferentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    [cell configWithData:[arrayTable objectAtIndex:indexPath.row] canUse:canUse];
    return cell;
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar                     // called when keyboard search button pressed
{
    NSLog( @"%s,%d" , __FUNCTION__ , __LINE__ );
    [searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

#pragma mark - HRProgress Delegate Methods

-(void)tapHUDButtonAction:(id)button{
    UIButton *theButton=(UIButton *)button;
    
    if([theButton.titleLabel.text isEqualToString:@"重新加载"]){
        [self loadData];
    }else{
        //去逛逛
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
